﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using BusinessObjectLayer;
using BusinessLogicLayer;
using System.Data.SqlClient;
using BusinessLogicLibrary;

namespace UserInterface
{
    public partial class DatabaseSetting : Form
    {
        ExceptionHandlerBOL _ExceptionHandlerBOL;
        string connnectionString = string.Empty;

        public DatabaseSetting()
        {
            InitializeComponent();
        }
        private void DatabaseSetting_Load(object sender, EventArgs e)
        {
            FillServerComboBox();
        }
        private void FillServerComboBox()
        {
            try
            {
                DataTable ServersTable = new DataTable();
                SqlDataSourceEnumerator servers = SqlDataSourceEnumerator.Instance;
                ServersTable = servers.GetDataSources();

                object[] obj = new object[ServersTable.Rows.Count];
                for (int i = 0; i < ServersTable.Rows.Count; i++)
                {
                    obj[i] = string.Format("{0}\\{1}", ServersTable.Rows[i]["ServerName"].ToString(), ServersTable.Rows[i]["InstanceName"].ToString());
                }
                serversComboBox.DataSource = obj;
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void FillDataBaseComboBox()
        {
            SetConnectionString("Master");
            databaseComboBox.DataSource = ServerBLL.Select(connnectionString);
        }
        private void setButton_Click(object sender, EventArgs e)
        {
            try
            {                
                FillDataBaseComboBox();
                securityGroupBox.Enabled = false;
                databaseComboBox.Enabled = true;
            }
            catch (Exception exception)
            {
                _ExceptionHandlerBOL = ExceptionHandlerBLL.HandleException(exception);
                noticeLabel.ForeColor = Color.Red;
                noticeLabel.Text = _ExceptionHandlerBOL.Title;
            }
        }
        private void SetConnectionString(string database)
        {
            if (passwordTextBox.Text != string.Empty && userNameTextBox.Text != string.Empty)
                connnectionString = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", serversComboBox.Text, database, userNameTextBox.Text, passwordTextBox.Text);
            else
                connnectionString = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True", serversComboBox.Text, database);
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            CloseForm();
        }
        private void CloseForm()
        {
            this.Close();
        }

        private void acceptButton_Click(object sender, EventArgs e)
        {
            if (ValidateItem())
            {
                SetConnectionString();
            }
        }
        private void SetConnectionString()
        {
            string key = "ConnectionString";
            SetConnectionString(databaseComboBox.SelectedText);
            string value = connnectionString;
            SettingBOL _SettingBOL = new SettingBOL(key, value, null);
            new SettingBLL().WriteKey(_SettingBOL);
        }
        private bool ValidateItem()
        {
            return databaseComboBox.SelectedIndex >= 0;
        }
    }
}
